package aliyun

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/services/alimt"
	"github.com/gogf/gf/v2/util/gconv"
)

// translateTextService 文字机器翻译服务
type translateTextService struct {
}

type TranslateECommerceReq struct {
	SourceLanguage string
	SourceText     string
	FormatType     string
	Scene          string
	TargetLanguage string
}

type TranslateECommerceResp struct {
	RequestId string
	Code      int
	Words     string
	WordCount uint
}

func (*translateTextService) TranslateECommerce(paramsReq *TranslateECommerceReq) (*TranslateECommerceResp, error) {
	if client, err := Mt.createClient(); err != nil {
		return nil, err
	} else {
		// 创建API请求并设置参数
		request := alimt.CreateTranslateECommerceRequest()
		// 等价于 request.PageSize = "10"
		request.Method = "POST"                           //设置请求
		request.FormatType = paramsReq.FormatType         //翻译文本的格式
		request.SourceLanguage = paramsReq.SourceLanguage //源语言
		request.SourceText = paramsReq.SourceText         //原文
		request.TargetLanguage = paramsReq.TargetLanguage //目标语言
		request.Scene = paramsReq.Scene                   //目标语言
		resp, err := client.TranslateECommerce(request)
		if err != nil {
			return nil, err
		}
		paramsResp := &TranslateECommerceResp{
			Code:      resp.Code,
			RequestId: resp.RequestId,
		}
		if resp.Code == 200 {
			paramsResp.Words = resp.Data.Translated
			paramsResp.WordCount = gconv.Uint(resp.Data.WordCount)
		}
		return paramsResp, nil
	}
}
